package middleware

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"task_queue/internal/response"
	"task_queue/internal/utils"
)

func TokenMiddleWare(c *gin.Context) {
	token := c.Request.Header.Get("x-token")
	if token == "" {
		response.Fail(c, http.StatusBadRequest, "token is null")
		c.Abort()
		return
	}
	tokenString := token[len("Bearer "):]
	claims, err := utils.ParseToken(tokenString)
	if err != nil {
		response.Fail(c, http.StatusBadRequest, "token解析失败")
		c.Abort()
		return
	}

	c.Set("user", claims)
	c.Next()

}
